/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.rms.recordstores.calendar;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;

import mtt.calendar.CalenderWeekGenerator;
import mtt.rms.dataObjects.Event;
import mtt.rms.recordstores.calendar.events.AddEventToRmsEvent;
import mtt.rms.recordstores.calendar.events.DeleteEventFromRmsEvent;
import mtt.rms.recordstores.calendar.events.GetCurrentWeekEvent;
import mtt.rms.recordstores.calendar.events.GetNumCalendarRecordsEvent;
import mtt.rms.recordstores.calendar.events.SetCurrenWeekEvent;
import mtt.rms.recordstores.calendar.events.SetNumCalendarRecordsEvent;
import mtt.rms.recordstores.calendar.events.UpdateEventInRmsEvent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.framework.rms.ObjectStore;

/**
 * 
 * 
 * @author jens.meiss
 */
public class RecordStoreCalendar extends ObjectStore {

	/**
	 * specify the name of the "Marks"-RecordStore name
	 */
	private static String name = "MTT_Kalender";

	private void addEventToRms(Event event) throws RecordStoreException,
			IOException {

		if ((event == null) || (event.getRecordId() > 0)) {
			return;
		}

		store(event);

	}

	private void deleteEventFromRms(Event event)
			throws RecordStoreNotOpenException, InvalidRecordIDException,
			RecordStoreException {

		if ((event == null) || (event.getRecordId() <= 0)) {
			return;
		}

		remove(event.getRecordId());
	}

	private void getCurrentWeek(GetCurrentWeekEvent getCurrentWeekEvent) {

		Week week = new Week();
		CalenderWeekGenerator.configureCurrentWeek(week);

		/*
		 * Events der Woche laden
		 */
		Vector vectorEvents = getVectorRecords(new Event(), null, null, false);

		Enumeration enumeration = vectorEvents.elements();
		while (enumeration.hasMoreElements()) {
			Event event = (Event) enumeration.nextElement();
			week.addEvent(event);
		}

		getCurrentWeekEvent.getEventListener().receiveEvent(
				new SetCurrenWeekEvent(week));

	}

	protected String getRecordStoreName() {
		return name;
	}

	public void init() {

		register(GetCurrentWeekEvent.class);
		register(AddEventToRmsEvent.class);
		register(UpdateEventInRmsEvent.class);
		register(DeleteEventFromRmsEvent.class);
		register(GetNumCalendarRecordsEvent.class);

		open();
	}

	public void receiveEvent(IEvent event) {

		try {
			if (event instanceof GetCurrentWeekEvent) {

				GetCurrentWeekEvent getCurrentWeekEvent = (GetCurrentWeekEvent) event;
				getCurrentWeek(getCurrentWeekEvent);

			} else if (event instanceof AddEventToRmsEvent) {

				AddEventToRmsEvent addEventToRmsEvent = (AddEventToRmsEvent) event;
				addEventToRms(addEventToRmsEvent.getEvent());

			} else if (event instanceof UpdateEventInRmsEvent) {

				UpdateEventInRmsEvent updateEventInRmsEvent = (UpdateEventInRmsEvent) event;
				updateEventInRms(updateEventInRmsEvent.getEvent());

			} else if (event instanceof DeleteEventFromRmsEvent) {

				DeleteEventFromRmsEvent deleteEventFromRmsEvent = (DeleteEventFromRmsEvent) event;
				deleteEventFromRms(deleteEventFromRmsEvent.getEvent());

			} else if (event instanceof GetNumCalendarRecordsEvent) {
				((GetNumCalendarRecordsEvent) event)
						.getEventListener()
						.receiveEvent(
								new SetNumCalendarRecordsEvent(getNumRecords()));
			}

		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void terminate() {
		close();
	}

	private void updateEventInRms(Event event)
			throws RecordStoreNotOpenException, InvalidRecordIDException,
			RecordStoreFullException, IOException, RecordStoreException {

		if ((event == null) || (event.getRecordId() <= 0)) {
			return;
		}

		update(event);
	}

}
